package com.huqing.icu.service.app.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huqing.icu.mapper.CommentMapper;
import com.huqing.icu.mapper.UserCommentLikeMapper;
import com.huqing.icu.model.UserCommentLike;
import com.huqing.icu.service.UserCommentLikeService;
import com.huqing.icu.utils.ParamValidateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


/**
 * @Description 用户点赞评论实现类
 * @Author huqing
 * @Date 2025/1/9 14:56
 **/
@Service
public class UserCommentLikeServiceImpl extends ServiceImpl<UserCommentLikeMapper, UserCommentLike> implements UserCommentLikeService {

    @Resource
    private UserCommentLikeMapper userCommentLikeMapper;
    @Resource
    private CommentMapper commentMapper;

    //点赞或取消点赞

    @Override
    public Integer likeComment(Long commentId, Long userId) {
        ParamValidateUtils.validateNotNull(commentId, "评论id不能为空");
        ParamValidateUtils.validateNotNull(userId, "用户id不能为空");
        UserCommentLike userCommentLike = lambdaQuery().eq(UserCommentLike::getCommentId, commentId).eq(UserCommentLike::getUserId, userId).one();
        //如果点赞行为不存在，则新增点赞行为
        if (userCommentLike == null) {
            userCommentLike = new UserCommentLike(userId, commentId);
            save(userCommentLike);

            //点赞之后，评论的点赞数得 + 1
            commentMapper.incrLikeNum(commentId);
        } else {
            //如果点赞行为存在，则删除行为，表示取消点赞
            userCommentLikeMapper.deleteById(userCommentLike.getId());

            //取消点赞之后，评论的点赞数得 -1
            commentMapper.decrLikeNum(commentId);
        }
        return 1;
    }
}
